using Autenticazione;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace BusinessObject
{
	public class Atleta
	{
		public string EMail;
		public long Id;
		public string FullName;
		public int Gender;
		public string BirthDayString;
		public int Belt;
		public float Weight;
		public string GrapplingLevel;
		public int MatchFullContact;
		public int MatchLowKick;
		public int MatchK1Rules;
		public Atleta()
		{
		}
		public Atleta(string email, long id, string fullname, int gender, string birthday, int belt, float weight, string grapplinglevel, int matchFullContact, int matchLowKick, int matchK1Rules)
		{
			this.EMail = email;
			this.Id = id;
			this.FullName = fullname.Replace("'", "''");
			this.Gender = gender;
			this.BirthDayString = birthday;
			this.Belt = belt;
			this.Weight = weight;
			this.GrapplingLevel = grapplinglevel;
			this.MatchFullContact = matchFullContact;
			this.MatchLowKick = matchLowKick;
			this.MatchK1Rules = matchK1Rules;
		}
		public DataTable GetTbAtleti(string email)
		{
			string strSQL = string.Format("select * from Atleta where Email = '{0}' order by FullName", email);
			DB db = new DB();
			return db.GetTable(strSQL);
		}
		public List<Atleta> GetAtleti(string email)
		{
			List<Atleta> atleti = new List<Atleta>();
			string strSQL = string.Format("select * from Atleta where Email = '{0}' order by FullName", email);
			DB db = new DB();
			SqlDataReader dr = db.Reader(strSQL);
			if (dr == null)
			{
				return atleti;
			}
			while (dr.Read())
			{
				atleti.Add(new Atleta(dr["Email"].ToString(), long.Parse(dr["Id"].ToString()), dr["FullName"].ToString(), int.Parse(dr["Gender"].ToString()), DateTime.Parse(dr["BirthDay"].ToString()).ToString("dd/MM/yyyy"), int.Parse(dr["Belt"].ToString()), float.Parse(dr["Weight"].ToString()), dr["GrapplingLevel"].ToString(), int.Parse(dr["MatchFullContact"].ToString()), int.Parse(dr["MatchLowKick"].ToString()), int.Parse(dr["MatchK1Rules"].ToString())));
			}
			db.CloseConnection();
			return atleti;
		}
		public bool DeleteAtleta()
		{
			string strSQL = string.Format("delete from Atleta where Id = {0}", this.Id.ToString());
			DB db = new DB();
			Fights f = new Fights();
			bool deleted = db.ExecuteNonQuery(strSQL);
			if (deleted)
			{
				deleted &= f.DeleteFights(this.EMail, this.Id);
			}
			return deleted;
		}
		public bool Salva()
		{
			string w = this.Weight.ToString().Replace(",", ".");
			string strSQL = string.Format("insert into Atleta (EMail, FullName, Gender, BirthDay, Belt, Weight, GrapplingLevel, MatchFullContact, MatchLowKick, MatchK1Rules) values ('{0}', '{1}', {2}, '{3}', {4}, {5}, '{6}', {7}, {8}, {9})", new object[]
			{
				this.EMail, 
				this.FullName, 
				this.Gender.ToString(), 
				this.BirthDayString, 
				this.Belt.ToString(), 
				w, 
				this.GrapplingLevel, 
				this.MatchFullContact.ToString(), 
				this.MatchLowKick.ToString(), 
				this.MatchK1Rules.ToString()
			});
			DB db = new DB();
			return db.ExecuteNonQuery(strSQL);
		}
		public bool Update()
		{
			string w = this.Weight.ToString().Replace(",", ".");
			string strSQL = string.Format("update Atleta set FullName = '{0}', Gender = {1}, BirthDay = '{2}', Belt = {3}, Weight = {4}, GrapplingLevel = '{5}', MatchFullContact = {6}, MatchLowKick = {7}, MatchK1Rules = {8} where Id = {9}", new object[]
			{
				this.FullName, 
				this.Gender.ToString(), 
				this.BirthDayString, 
				this.Belt.ToString(), 
				w, 
				this.GrapplingLevel, 
				this.MatchFullContact.ToString(), 
				this.MatchLowKick.ToString(), 
				this.MatchK1Rules.ToString(), 
				this.Id.ToString()
			});
			DB db = new DB();
			return db.ExecuteNonQuery(strSQL);
		}
	}
}
